package yxy.game.pm2.command.capturestrategicpoints;


import org.joda.time.DateTime;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CaptureStrategicPointsMessage;
import yxy.game.pm2.module.capturestrategicpoints.CaptureStrategicPointsHandle;
import yxy.game.pm2.module.capturestrategicpoints.CaptureStrategicPointsModule;

import java.util.ArrayList;

/**
 * @author wrs
 * @descrition 斩关夺隘挑战
 * @since 2022-08-17
 */
@MessageType(proto = Command.CaptureStrategicPointsChallenge, classtype = CaptureStrategicPointsMessage.CaptureStrategicPointsChallengeRequest.class)
public class CMD_CaptureStrategicPointsChallenge extends Command<CaptureStrategicPointsMessage.CaptureStrategicPointsChallengeRequest> {

    @Override
    public void execute(MessageSource messageSource, CaptureStrategicPointsMessage.CaptureStrategicPointsChallengeRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        CaptureStrategicPointsMessage.CaptureStrategicPointsChallengeResponse.Builder response = CaptureStrategicPointsMessage.CaptureStrategicPointsChallengeResponse.newBuilder();
        int day = request.getDay(); //第几天
        boolean skip = request.getSkip(); //是否跳过战斗
        try {
            CaptureStrategicPointsModule module = CaptureStrategicPointsModule.get(playerCode);
            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            if (module.getDay() < day) {
                send(messageSource, response, Result.DataError);
                return;
            }
            CaptureStrategicPointsHandle handle = new CaptureStrategicPointsHandle();

            //todo 1.返回录像信息和是否胜利
            handle.battleResponseExecute(playerCode, module, day, response);

            //todo 2.领取的过关达标奖励
            ArrayList<Integer> boxMap = module.getBoxMap();
            response.addAllBoxes(boxMap);

            //todo 3.守将信息和通关奖励
            ArrayList<CaptureStrategicPointsMessage.OneDayMsgModule.Builder> list = handle.getOneDayMsgModule(module);
            for (CaptureStrategicPointsMessage.OneDayMsgModule.Builder builder : list) {
                response.addMsgModules(builder);
            }

            //todo 4.结束时间,当前天数，总通关数
            int num = handle.getPassCustomNum(module);
            response.setTotalPassNum(num);//总通关数
            response.setDay(module.getDay());//当前天数
            DateTime endTime = module.getEndTime();
            response.setEndTime(endTime.getMillis());//结束时间

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, CaptureStrategicPointsMessage.CaptureStrategicPointsChallengeResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
